﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		StripWhiteSpaceFilter.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.IO;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using Dexter.Dependency;
using Dexter.Services;

namespace Dexter.Web.Mvc.Filters {
	[AttributeUsage ( AttributeTargets.Class | AttributeTargets.Method , Inherited = true , AllowMultiple = false )]
	[AspNetHostingPermission ( SecurityAction.Demand , Level = AspNetHostingPermissionLevel.Minimal )]
	public class StripWhiteSpaceFilter : ActionFilterAttribute {
		readonly IConfigurationService configuration;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:System.Web.Mvc.ActionFilterAttribute"/> class.
		/// </summary>
		public StripWhiteSpaceFilter ( IConfigurationService configuration ) {
			this.configuration = configuration;
		}

		public override void OnActionExecuting ( ActionExecutingContext filterContext ) {
			base.OnActionExecuting ( filterContext );

			var siteConfiguration = configuration.Configuration;

			if ( !siteConfiguration.AdvancedFeatues.StripWhiteSpace ) {
				return;
			}

			filterContext.HttpContext.Response.Filter = new WhitespaceFilter ( filterContext.HttpContext.Response.Filter );
		}
	}

	internal class WhitespaceFilter : Stream {
		static readonly Regex regexBetweenTags = new Regex ( @">\s+<" , RegexOptions.Compiled );
		static readonly Regex regexLineBreaks = new Regex ( @"\n\s+" , RegexOptions.Compiled );
		readonly Stream sink;

		public WhitespaceFilter ( Stream sink ) {
			this.sink = sink;
		}

		public override void Flush ( ) {
			sink.Flush ( );
		}

		public override long Seek ( long offset , SeekOrigin origin ) {
			return sink.Seek ( offset , origin );
		}

		public override void SetLength ( long value ) {
			sink.SetLength ( value );
		}

		public override void Close ( ) {
			sink.Close ( );
		}

		public override int Read ( byte[] buffer , int offset , int count ) {
			return sink.Read ( buffer , offset , count );
		}

		public override void Write ( byte[] buffer , int offset , int count ) {
			var data = new byte[count];
			Buffer.BlockCopy ( buffer , offset , data , 0 , count );
			string html = Encoding.Default.GetString ( buffer );

			html = regexBetweenTags.Replace ( html , "> <" );
			html = regexLineBreaks.Replace ( html , string.Empty );
			html = html.Replace ( "\r" , string.Empty );
			html = html.Replace ( "//<![CDATA[" , string.Empty );
			html = html.Replace ( "//]]>" , string.Empty );
			html = html.Replace ( "\n" , string.Empty );

			byte[] outdata = Encoding.Default.GetBytes ( html.Trim ( ) );
			sink.Write ( outdata , 0 , outdata.GetLength ( 0 ) );
		}

		#region Properites

		public override bool CanRead {
			get { return true; }
		}

		public override bool CanSeek {
			get { return true; }
		}

		public override bool CanWrite {
			get { return true; }
		}

		public override long Length {
			get { return 0; }
		}

		public override long Position { get; set; }

		#endregion
	}
}
